﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GuiConny;
using System.Threading;
 

namespace MKIB3Game 
{

    public class Backend : IBackend
    {
       //public für testzwecke
       public IParser parser; 
        Map map; 
        List<Message> chat ; 
        List<Figure> player; 
        List<Figure> dragon;  
        Result result;
        long time; 
        int online; 
        int yourID;
        ActionEnum decision;
        AnswerEnum answer;
        Challenge challenge;
        MyMainWindow gui;


        public Backend(MyMainWindow gui)
            : this()
        {
            this.gui = gui;

        }
        public Backend()
        {

            chat = new List<Message>();
            player = new List<Figure>();
            dragon = new List<Figure>(); 
            parser = new Parser((IBackend)this);
           

        }
         
        public void setChallenge(Challenge c)
        {
            this.challenge = c;
        }
        public Challenge getChallenge()
        {
            return this.challenge;
        }
        public void setAnswer(AnswerEnum a)
        {
            this.answer = a;
        }
      
        public AnswerEnum getAnswer()
        {
            return this.answer;
        }
        public void upd(Figure f)
        {
            addFigure(f);
            gui.setDragons(dragon);
            gui.setPlayers(player);
            gui.updateMe();
        }
        private void addFigure(Figure f)
        {

            if(f.getType().Equals(FigureTypeEnum.PLAYER)){
                bool notInList = true;
                foreach(Figure g in this.player){
                    if (g.Equals(f))
                    {
                        notInList = false;
                        g.updateAttributesTo(f);
                    }
                }
                if (notInList)
                    player.Add(f);              

            }
            else if(f.getType().Equals(FigureTypeEnum.DRAGON)){
                  bool notInList = true;
                foreach(Figure g in this.dragon){
                    if (g.Equals(f))
                    {
                        notInList = false;
                        g.updateAttributesTo(f);
                    }
                }
                if (notInList)
                    dragon.Add(f);  
            }
            else{
                Console.WriteLine("unknown type of figure");
            }


        }
        public void upd(MapCell mapcell)
        {
            this.map.setMapCell(mapcell);
        }
        public void del(Figure f)
        {
            switch (f.getType())
            {
                case FigureTypeEnum.PLAYER: player.Remove(f); break;  // TESTEN!!!!!!!!
                case FigureTypeEnum.DRAGON: dragon.Remove(f); break;
                default: Console.WriteLine("unknown type of figure"); break;
            }
        }
        public void setMyFigure(Figure f)
        {
            this.yourID = f.getID();
            this.upd(f);
            Console.WriteLine("eigene figur wurde gesetzt, die id ist: " + this.yourID);
            gui.updateMe();
        }
        public ActionEnum getDecision()
        {
            return this.decision;
        }
        public void setDecision(ActionEnum d)
        {
            this.decision = d;
        }
        public int getYourID()
        {
            return this.yourID;
        }
        public void setYourID(int id)
        {
            this.yourID = id;
        }
        public int getOnline()
        {
            return this.online;
        }
        public void setOnline(int o)
        {
            this.online = o;
        }

        public void setTime(long t)
        {
            this.time = t;
        }
        public long getTime()
        {
            return this.time;
        }
        public void setResult(Result r)
        {
            this.result = r;
        }
        public Result getResult()
        {
            return this.result;
        }
        public void addChatMessage(Message m)
        {            
            chat.Add(m);
            gui.setMessageBuffer(chat);
            gui.update_Chat();
        }
        public List<Message> getChat()
        {
            return chat;
        }
        public void setMap(Map map)
        {
            this.map = map;
            gui.setMap(map);
        }
        public void setMap(int width, int heigth){
            this.map = new Map(width,heigth);
        }
        public Map getMap()
        {
            return map;
        }
        public void setMapCell(MapCell mapcell)
        {
            this.map.setMapCell(mapcell);
        }
        
        public Figure getMe()
        {
            Figure f;
            foreach(Figure e in this.player){
                if(e.getID() == this.getYourID()){
                 return e;
                }
            }
            return null;
        }

        // rückwärts methoden
     


        public void move(MovementEnum mov)
        {
            parser.parseToString(mov);
        }
        public void sendMessage(string message)
        {
            Message m = new Message(this.yourID, "unknown", message); 
            parser.parseToString(m);         
        }
        public void sendServerMessage(String servmes)
        {
            parser.parseToString(servmes);
        }
        public void askServer(ServerEnum se){
            parser.parseToString(se);
        }
        public void challangeAction(ActionEnum ae)
        {
            parser.parseToString(ae);
        }
        public void getEntityFromServer(int id)
        {
            parser.parseToString(id);
        }
        public void invitePlayerToChallange(ChallengeTypeEnum challenge, int id){
            Object[] ar = { challenge, id };
            parser.parseToString(ar);
        }
        public void RenameMe(String newName)
        {
             Object[] ar = { ServerEnum.RENAME, newName };
             parser.parseToString(ar);
        }
    }


}